<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import { ElMessageBox } from "element-plus";
import { fa, tr } from "element-plus/es/locale/index.js";
import type { FormInstance, FormRules } from "element-plus";
import { perList, peradd, perupdate, perdel } from "../api/axios.js";
// 表单绑定
const ruleForm = reactive({
  name: "",
  code: "",
  description: "",
  type: 0,
  pid: 0,
  enVisible: "0",
});
let perupid = ref("");
let permList = ref([]);
// 初始化
onMounted(() => {
  res();
});
// 获取数据列表
const res = async () => {
  let list = await perList();
  console.log(list.data.data);
  permList.value = filterList(list.data.data);
};
// 对列表进行树形结构
let filterList = (data: any, id = 0) => {
  let newArr: any = [];
  let oldArr = [];
  data.forEach((item: any) => {
    if (item.pid == id) {
      oldArr = filterList(data, item.id);
      if (oldArr.length > 0) {
        item.children = oldArr;
      }
      newArr.push(item);
    }
  });
  return newArr;
};
// 声明两个弹框开关
const dialogVisible = ref(false);
const dialogVisible1 = ref(false);
// 点击创新时将表单清空
const permadd = () => {
  perupid.value = "";
  ruleForm.name = "";
  ruleForm.code = "";
  ruleForm.description = "";
  ruleForm.enVisible = "0";
  dialogVisible.value = true;
};
// 表单非空验证
const ruleFormRef = ref<FormInstance>();
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("权限名称不能为空"));
  } else {
    if (ruleForm.name !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("权限描述不能为空"));
  } else {
    callback();
  }
};

const rules = reactive<FormRules<typeof ruleForm>>({
  name: [{ validator: validatePass, trigger: "blur" }],
  code: [{ validator: validatePass2, trigger: "blur" }],
});
// 控制表单内地开关
const change1 = () => {
  if (ruleForm.enVisible == 1) {
    ruleForm.enVisible = 0;
  } else {
    ruleForm.enVisible = 1;
  }
  console.log(ruleForm.enVisible);
};
// 根据判断是否有修改id来进行添加或修改
const peraddok = async () => {
  if (perupid.value == "") {
    await peradd(ruleForm);
  } else {
    let obj = ruleForm;
    obj.id = perupid.value;
    console.log(perupid.value, obj);

    await perupdate(perupid.value, obj);
  }
  dialogVisible.value = false;
  console.log(ruleForm);
  res();
};
// 向一个权限下添加分支权限
const perpadd = (iid) => {
  perupid.value = "";
  ruleForm.name = "";
  ruleForm.code = "";
  ruleForm.pid = iid;
  ruleForm.description = "";
  ruleForm.enVisible = "0";
  dialogVisible.value = true;
};
// 修改时回显列表
const perupda = (item) => {
  perupid.value = item.id;
  ruleForm.name = item.name;
  ruleForm.code = item.code;
  ruleForm.pid = item.pid;
  ruleForm.description = item.description;
  ruleForm.enVisible = "0";
  dialogVisible.value = true;
};
// 删除时弹出弹框
const perdelc = (iid) => {
  perupid.value = iid;
  dialogVisible1.value = true;
};
// 确认删除
const perdelok = async () => {
  await perdel(perupid.value);
  res();
  dialogVisible1.value = false;
};
</script>

<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      title="添加角色"
      width="500"
      :before-close="handleClose"
    >
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="ruleForm.name" type="text" autocomplete="off" />
        </el-form-item>

        <el-form-item label="权限标识" prop="code">
          <el-input v-model="ruleForm.code" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input
            v-model="ruleForm.description"
            type="text"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="启用" prop="checkPass">
          <el-switch :model-value="ruleForm.enVisible == 0" @change="change1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="peraddok()"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      v-model="dialogVisible1"
      title="删除角色"
      width="500"
      :before-close="handleClose"
    >
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible1 = false">取消</el-button>
          <el-button type="primary" @click="perdelok()"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
    <el-button type="primary" color="#2752fb" @click="permadd()"
      >添加权限</el-button
    >

    <el-table
      :data="permList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="code" label="标识" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作" #default="props">
        <p v-show="!props.row.pid">
          <el-button style="color: #409eff" link @click="perpadd(props.row.id)">
            添加
          </el-button>
          <el-button style="color: #409eff" link @click="perupda(props.row)">
            编辑
          </el-button>
          <el-button style="color: #409eff" link @click="perdelc(props.row.id)">
            删除
          </el-button>
        </p>
        <p v-show="props.row.pid">
          <el-button style="color: #409eff" link> 编辑 </el-button>
          <el-button style="color: #409eff" link @click="perdelc(props.row.id)">
            删除
          </el-button>
        </p>
      </el-table-column>
    </el-table>
  </div>
</template>

<style lang="less" scoped>
</style>
